package com.peas.platform.module.icims.budget.ysy.service.impl;

import cn.hutool.core.lang.Assert;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.budget.ysy.component.BudgetYsyGuangzhouClient;
import com.peas.platform.module.icims.budget.ysy.domain.BudgetYsyLoginDomain;
import com.peas.platform.module.icims.budget.ysy.dto.BudgetYsyConfigDto;
import com.peas.platform.module.icims.budget.ysy.dto.BudgetYsyLoginDto;
import com.peas.platform.module.icims.budget.ysy.service.BudgetYsyConfigService;
import com.peas.platform.module.icims.budget.ysy.service.BudgetYsyLoginService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BudgetYsyLoginServiceImpl extends BaseServiceImpl<BudgetYsyLoginDto> implements BudgetYsyLoginService {

    @Value("${ysy.username}")
    private String ysyUsername;
    @Value("${ysy.password}")
    private String ysyPassword;

    @Resource
    private BudgetYsyLoginDomain budgetYsyLoginDomain;
    @Resource
    private BudgetYsyConfigService ysyConfigService;
    @Resource
    private BudgetYsyGuangzhouClient budgetYsyGuangzhouClient;

    @Override
    public BaseDomain<BudgetYsyLoginDto, ? extends BasePo> getDomain() {
        return budgetYsyLoginDomain;
    }

    @Transactional
    @Override
    public BudgetYsyLoginDto loginYsy() {
        List<BudgetYsyConfigDto> configs = ysyConfigService.list(new BudgetYsyConfigDto());
        Assert.notEmpty(configs, "请先添加预算域配置");
        BudgetYsyConfigDto ycd = configs.get(0);
        String url = ycd.getAddress() + ycd.getPort() + ycd.getLogin();
        BudgetYsyLoginDto dto = budgetYsyGuangzhouClient.login(url, ysyUsername, ysyPassword);
        this.save(dto);
        return dto;
    }

}
